﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<link href="~/css/site.css" rel="stylesheet" />
<link href="~/layui/css/layui.css" rel="stylesheet" />

<h1>修改开票管理</h1>
<div class="layui-container" style="padding:20px">
    <div class="layui-row">
        <div class="layui-col-md12">
            <h2>修改开票管理</h2>
        </div>
    </div>
    <div class="layui-row">
        <div class="layui-col-md8 layui-col-md-offset2">
            <form class="layui-form" id="UpdateOpenFare">
                <input type="hidden" id="openFareId" name="openFareId">
                <div class="layui-form-item">
                    <label>开票编号</label>
                    <div class="layui-input-block">
                        <input type="text" id="openFareCode" name="openFareCode" lay-verify="required" placeholder="请输入开票编号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label>开票主题</label>
                    <div class="layui-input-block">
                        <input type="text" id="openFareTheme" name="openFareTheme" lay-verify="required" placeholder="请输入开票主题" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label>发票类型</label>
                    <div class="layui-input-block">
                        <input type="text" id="invoiceTypeName" name="invoiceTypeName" lay-verify="required" placeholder="请输入发票类型" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label>开票日期</label>
                    <div class="layui-input-block">
                        <input type="date" id="openFareTime" name="openFareTime" lay-verify="required" placeholder="请输入开票日期" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label>收票方</label>
                    <div class="layui-input-block">
                        <input type="text" id="collectTickets" name="collectTickets" lay-verify="required" placeholder="请输入收票方" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label>开票金额</label>
                    <div class="layui-input-block">
                        <input type="text" id="openFareMoney" name="openFareMoney" lay-verify="required" placeholder="请输入开票金额" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label>发票号</label>
                    <div class="layui-input-block">
                        <input type="text" id="invoiceTypeCode" name="invoiceTypeCode" lay-verify="required" placeholder="请输入发票号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label>备注</label>
                    <div class="layui-input-block">
                        <input type="text" id="openFareRemark" name="openFareRemark" placeholder="请输入备注" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="UpdateOpenFare">
                            <i class="layui-icon layui-icon-ok"></i> 提交
                        </button>
                        <button type="reset" class="layui-btn layui-btn-primary">
                            <i class="layui-icon layui-icon-refresh"></i> 重置
                        </button>
                        <button type="button" class="layui-btn layui-btn-warm" id="backToList">
                            <i class="layui-icon layui-icon-left"></i> 返回列表
                        </button>

                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 先引入jQuery，再引入其他脚本 -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="~/js/site.js"></script>
<script src="~/layui/layui.js"></script>

<script>
    $(function() {
        // 页面加载时获取ID并反填数据
        var openFareId = localStorage.getItem("openFareId");
        if (openFareId) {
            openFareFantian(openFareId);
        }
        
        // 绑定返回按钮事件
        $('#backToList').click(function() {
            window.location.href = '/OpenFare/ShowOpenFare';
        });
        
        // 绑定表单提交事件
        layui.use(['form', 'layer'], function(){
            var form = layui.form;
            var layer = layui.layer;
            
            form.on('submit(UpdateOpenFare)', function(data){
                var formData = data.field;
                updateOpenFare(formData);
                return false; // 阻止表单默认提交
            });
        });
    });

    function openFareFantian(openFareId) {
        $.ajax({
            url: 'http://localhost:5054/api/OpenFare/FanTianOpenFare?OpenFareId=' + openFareId,
            dataType: 'json',
            type: 'get',
            contentType: 'application/json',
            success: function(res) {
                console.log('API返回数据:', res); // 调试用
                if (res && res.code == 200 && res.data) {
                    var data = res.data;
                    // 如果data是数组，取第一个元素
                    if (Array.isArray(data) && data.length > 0) {
                        data = data[0];
                    }
                    
                    // 反填表单数据
                    $('#openFareId').val(data.openFareId);
                    $('#openFareCode').val(data.openFareCode);
                    $('#openFareTheme').val(data.openFareTheme);
                    $('#invoiceTypeName').val(data.invoiceTypeName);
                    $('#openFareTime').val(data.openFareTime);
                    $('#collectTickets').val(data.collectTickets);
                    $('#openFareMoney').val(data.openFareMoney);
                    $('#invoiceTypeCode').val(data.invoiceTypeCode);
                    $('#openFareRemark').val(data.openFareRemark);
                    
                    layer.msg("数据加载成功！");
                } else {
                    layer.msg("获取数据失败！");
                }
            },
            error: function(xhr, status, error) {
                console.error('获取数据失败:', error);
                layer.msg("获取数据失败，请检查网络连接！");
            }
        });
    }
    
    // 更新开票数据
    function updateOpenFare(formData) {
        $.ajax({
            url: 'http://localhost:5054/api/OpenFare/UpdateOpenFare',
            dataType: 'json',
            type: 'post',
            contentType: 'application/json',
            data: JSON.stringify(formData),
            success: function(res) {
                if (res && res.code == 200) {
                    layer.msg("修改成功！");
                    setTimeout(function() {
                        window.location.href = '/OpenFare/ShowOpenFare';
                    }, 1500);
                } else {
                    layer.msg("修改失败：" + (res.message || "未知错误"));
                }
            },
            error: function(xhr, status, error) {
                console.error('修改失败:', error);
                layer.msg("修改失败，请检查网络连接！");
            }
        });
    }
</script>